package com.edooteach.tencent.handle.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.edooteach.tencent.entity.corpmessage.CorpMessageContent;
import com.edooteach.tencent.entity.corpmessage.CorpMessageRequest;
import com.edooteach.tencent.entity.corpmessage.CorpMessageType;
import com.edooteach.tencent.entity.corpmessage.CorpNewsMessage;
import com.edooteach.tencent.handle.CorpMessageHandler;
import com.edooteach.tencent.rao.CorpMessageRao;

/**
 * 
 * @author mohanlan
 *
 */
@Component("corpMessageHandler")
public class CorpMessageHandlerImpl implements CorpMessageHandler {

	@Rao
	private CorpMessageRao corpMessageRao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.edooteach.tencent.handle.CorpMessageHandler#send(com.edooteach.tencent.
	 * entity.corpmessage.CorpMessageRequest)
	 */
	public HttpResult send(String accessToken, CorpMessageRequest request) {

		// load type
		CorpMessageType type = request.getType();

		// decide send method
		if (type == CorpMessageType.TEXT || type == CorpMessageType.IMAGE || type == CorpMessageType.VOICE
				|| type == CorpMessageType.FILE) {
			return this.sendBasicMessage(accessToken, request);
		} else if (type == CorpMessageType.VIDEO) {
			return this.sendVideoMessage(accessToken, request);
		} else if (type == CorpMessageType.NEWS) {
			return this.sendNewsMessage(accessToken, request);
		} else if (type == CorpMessageType.MPNEWS) {
			return this.sendMpnewsMessage(accessToken, request);
		}

		throw BusinessException.newInstance("400", "参数异常");
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	private Map<String, Object> generateParams(CorpMessageRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("touser", request.getSendto());
		params.put("msgtype", request.getType().getAlias());
		params.put("agentid", "1");
		params.put("safe", "0");

		return params;
	}

	/**
	 * 
	 * @param content
	 * @return
	 */
	private boolean validateMessage(CorpMessageContent content) {
		return StringUtils.isNotEmpty(content.getContent());
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	private HttpResult sendBasicMessage(String accessToken, CorpMessageRequest request) {

		CorpMessageContent content = request.getContent();

		if (!this.validateMessage(content)) {
			throw BusinessException.newInstance("400", "文本类型消息参数异常");
		}

		Map<String, Object> params = this.generateParams(request);
		Map<String, Object> text = new HashMap<String, Object>();

		text.put(request.getType().getParams(), content.getContent());
		params.put(request.getType().getAlias(), text);

		HttpResult httpResult = corpMessageRao.send(accessToken, params);

		return httpResult;
	}

	private boolean validateVideoMessage(CorpMessageContent content) {
		return StringUtils.isNotEmpty(content.getContent());
	}

	private HttpResult sendVideoMessage(String accessToken, CorpMessageRequest request) {

		CorpMessageContent content = request.getContent();

		if (!this.validateVideoMessage(content)) {
			throw BusinessException.newInstance("400", "视频类型消息参数异常");
		}

		Map<String, Object> params = this.generateParams(request);

		Map<String, Object> video = new HashMap<String, Object>();
		video.put("title", request.getContent().getVideoTitle());
		video.put("description", request.getContent().getVideoDescription());
		video.put(request.getType().getParams(), request.getContent().getContent());

		params.put(request.getType().getAlias(), video);

		HttpResult httpResult = corpMessageRao.send(accessToken, params);

		return httpResult;
	}

	/**
	 * @param content
	 * @return
	 */
	private boolean validateNewsMessage(CorpMessageContent content) {
		List<CorpNewsMessage> newsMessages = content.getNews();
		for (CorpNewsMessage corpNewsMessage : newsMessages) {
			return StringUtils.isNotEmpty(corpNewsMessage.getTitle())
					&& StringUtils.isNotEmpty(corpNewsMessage.getDescription())
					&& StringUtils.isNotEmpty(corpNewsMessage.getUrl())
					&& StringUtils.isNotEmpty(corpNewsMessage.getPicture());
		}
		return false;
	}

	/**
	 * @param request
	 * @return
	 */
	private HttpResult sendNewsMessage(String accessToken, CorpMessageRequest request) {
		CorpMessageContent content = request.getContent();

		Map<String, Object> params = this.generateParams(request);

		Map<String, Object> news = new HashMap<String, Object>();
		List<Map<String, Object>> articles = new ArrayList<Map<String, Object>>();

		for (CorpNewsMessage newsItem : content.getNews()) {

			if (!this.validateNewsMessage(content)) {
				throw BusinessException.newInstance("400", "新闻类型消息参数异常");
			}

			Map<String, Object> item = new HashMap<String, Object>();
			item.put("title", newsItem.getTitle());
			item.put("description", newsItem.getDescription());
			item.put("url", newsItem.getUrl());
			item.put("picurl", newsItem.getPicture());
			articles.add(item);
		}

		news.put(request.getType().getParams(), articles);
		params.put(request.getType().getAlias(), news);

		HttpResult httpResult = corpMessageRao.send(accessToken, params);

		return httpResult;
	}

	/**
	 * 
	 * @param content
	 * @return
	 */
	private boolean validateMpnewsMessage(CorpMessageContent content) {
		List<CorpNewsMessage> newsMessages = content.getNews();
		for (CorpNewsMessage corpNewsMessage : newsMessages) {
			return StringUtils.isNotEmpty(corpNewsMessage.getTitle())
					&& StringUtils.isNotEmpty(corpNewsMessage.getDescription())
					&& StringUtils.isNotEmpty(corpNewsMessage.getUrl())
					&& StringUtils.isNotEmpty(corpNewsMessage.getPicture())
					&& StringUtils.isNotEmpty(corpNewsMessage.getAuthor())
					&& StringUtils.isNotEmpty(corpNewsMessage.getCover())
					&& StringUtils.isNotEmpty(corpNewsMessage.getContent());
		}
		return false;
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	private HttpResult sendMpnewsMessage(String accessToken, CorpMessageRequest request) {
		CorpMessageContent content = request.getContent();

		if (!this.validateMpnewsMessage(content)) {
			throw BusinessException.newInstance("400", "新闻类型消息参数异常");
		}

		Map<String, Object> params = this.generateParams(request);

		Map<String, Object> mpnews = new HashMap<String, Object>();
		List<Map<String, Object>> articles = new ArrayList<Map<String, Object>>();

		for (CorpNewsMessage newsItem : content.getNews()) {

			if (!this.validateNewsMessage(content)) {
				throw BusinessException.newInstance("400", "新闻类型消息参数异常");
			}
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("title", newsItem.getTitle());
			item.put("description", newsItem.getDescription());
			item.put("url", newsItem.getUrl());
			item.put("picurl", newsItem.getPicture());
			item.put("author", newsItem.getAuthor());
			item.put("show_cover_pic", newsItem.getCover());
			item.put("content", newsItem.getContent());
			articles.add(item);
			params.put(request.getType().getAlias(), mpnews);
		}
		HttpResult httpResult = corpMessageRao.send(accessToken, params);
		return httpResult;
	}
}
